package cn.iocoder.yudao.module.jikong.controller.app.order;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.dal.redis.no.ErpNoRedisDAO;
import cn.iocoder.yudao.module.jikong.controller.admin.address.vo.AddressRespVO;
import cn.iocoder.yudao.module.jikong.controller.admin.jkOrder.vo.JkOrderPageReqVO;
import cn.iocoder.yudao.module.jikong.controller.admin.jkOrder.vo.JkOrderSaveReqVO;
import cn.iocoder.yudao.module.jikong.controller.admin.jkProduct.vo.JkProductRespVO;
import cn.iocoder.yudao.module.jikong.controller.app.order.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.jkOrder.JkOrderDO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.jkProduct.JkProductDO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.user.JkUserDO;
import cn.iocoder.yudao.module.jikong.service.address.AddressService;
import cn.iocoder.yudao.module.jikong.service.jkOrder.JkOrderService;
import cn.iocoder.yudao.module.jikong.service.jkProduct.JkProductService;
import cn.iocoder.yudao.module.jikong.service.jkProductType.JkProductTypeService;
import cn.iocoder.yudao.module.jikong.service.user.JkUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "WEB申请单据等接口")
@RestController
@RequestMapping("/jikong/jk-order")
@Validated
@Slf4j
public class JkWebOrderController {

    @Resource
    private JkOrderService jkOrderService;
    @Resource
    private JkUserService jkUserService;
    @Resource
    private AddressService addressService;
    @Resource
    private ErpNoRedisDAO noRedisDAO;
    @Resource
    private JkProductService jkProductService;
    @Resource
    private JkProductTypeService jkProductTypeService;


    @GetMapping("/page")
    @Operation(summary = "获得单据分页")
    public CommonResult<PageResult<JkOrderRespVO>> getJkOrderPage(@Valid JkAppOrderPageReqVO pageReqVO) {
        JkOrderPageReqVO jkOrderPageReqVO = new JkOrderPageReqVO();
        BeanUtils.copyProperties(pageReqVO, jkOrderPageReqVO);
        jkOrderPageReqVO.setUserId(getLoginUserId());
        PageResult<JkOrderDO> pageResult = jkOrderService.getJkOrderPage(jkOrderPageReqVO);
        PageResult<JkOrderRespVO> respPageResult = BeanUtils.toBean(pageResult, JkOrderRespVO.class);
        respPageResult.getList().forEach(order -> {
            JkProductDO jkProduct = jkProductService.getJkProduct(order.getProductId());
            JkProductRespVO bean = BeanUtils.toBean(jkProduct, JkProductRespVO.class);
            bean.setTypeName(jkProductTypeService.getJkProductType(bean.getTypeId()).getName());
            order.setProductData(bean);
        });
        return success(respPageResult);
    }

    @GetMapping("/get")
    @Operation(summary = "获得单据详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<JkOrderRespVO> getJkOrder(@RequestParam("id") Long id) {
        JkOrderDO jkOrder = jkOrderService.getJkOrder(id);
        JkProductDO jkProduct = jkProductService.getJkProduct(jkOrder.getProductId());
        JkProductRespVO bean = BeanUtils.toBean(jkProduct, JkProductRespVO.class);
        bean.setTypeName(jkProductTypeService.getJkProductType(bean.getTypeId()).getName());
        JkOrderRespVO result = BeanUtils.toBean(jkOrder, JkOrderRespVO.class);
        result.setProductData(bean);
        return success(result);
    }



    @PostMapping("/applyOrder")
    @Operation(summary = "申请样本")
    public CommonResult<Long> createJkOrder(@Valid @RequestBody JkAppOrderSaveReqVO createReqVO) {
        JkOrderSaveReqVO saveReq = new JkOrderSaveReqVO();
        BeanUtils.copyProperties(createReqVO, saveReq);

        JkUserDO user = jkUserService.getUser(getLoginUserId());

        saveReq.setUserId(getLoginUserId());
        saveReq.setStatus(1);
        saveReq.setUserType(user.getUserType());
        saveReq.setContactName(createReqVO.getContactName());
        saveReq.setContactPhone(createReqVO.getContactPhone());
        saveReq.setAddress(createReqVO.getAddress());
        saveReq.setOrderNo(noRedisDAO.generate("SQ"));
        Long jkOrder = jkOrderService.createJkOrder(saveReq);
        return success(jkOrder);
    }

    @PostMapping("/applyAgreement")
    @Operation(summary = "上传协议-只有3和9可以调用")
    public CommonResult<Boolean> createJkOrder(@Valid @RequestBody JkAppOrderApplyAgreementVO createReqVO) {
        JkOrderSaveReqVO saveReq = new JkOrderSaveReqVO();
        BeanUtils.copyProperties(createReqVO, saveReq);
        saveReq.setStatus(4);
        jkOrderService.updateJkOrder(saveReq);
        return success(true);
    }


    @PostMapping("/cancelOrder")
    @Operation(summary = "取消申请")
    public CommonResult<Boolean> createJkOrder(@Valid @RequestBody JkCancelOrderStatusReqVO cancelReqVO) {
        JkOrderSaveReqVO saveReq = new JkOrderSaveReqVO();
        saveReq.setId(cancelReqVO.getId());
        saveReq.setStatus(100);
        jkOrderService.updateJkOrder(saveReq);
        return success(true);
    }

    @GetMapping("/getAddress")
    @Operation(summary = "获得地址管理")
    @PermitAll
    public CommonResult<AddressRespVO> getAddress() {
        AddressDO address = addressService.getAddress(1L);
        return success(BeanUtils.toBean(address, AddressRespVO.class));
    }


}
